package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.LoginActivity;
import com.jjwxc.jwjskandriod.activity.MyPurchaseActivity;
import com.jjwxc.jwjskandriod.activity.RechargeActivity;
import com.jjwxc.jwjskandriod.activity.RechargeCoinActivity;
import com.jjwxc.jwjskandriod.activity.SettingActivity;
import com.jjwxc.jwjskandriod.activity.VipInfoActivity;
import com.jjwxc.jwjskandriod.activity.WebActivity;
import com.jjwxc.jwjskandriod.adapter.MyAdapter;
import com.jjwxc.jwjskandriod.config.Constants;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFActivity;
import com.jjwxc.jwjskandriod.framework.base.FFBaseActivity;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ListUserBookRequest;
import com.jjwxc.jwjskandriod.model.UserBalanceResponse;
import com.jjwxc.jwjskandriod.model.UserProfileRequest;
import com.jjwxc.jwjskandriod.model.UserTotalresponse;
import com.jjwxc.jwjskandriod.my.activity.SelectAvatarActivity;
import com.jjwxc.jwjskandriod.my.data.UserInfoBg;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.utils.SharedPreUtils;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.jjwxc.jwjskandriod.widget.AutoHeightViewPager;
import com.jjwxc.jwjskandriod.widget.BuyMonthlyPop;
import com.jjwxc.jwjskandriod.widget.InterestPop;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;

public class MyFragment extends Fragment implements View.OnClickListener {
    private View view;
    AutoHeightViewPager viewPager;
    TabLayout tabLayout;
    private ImageView img_setting;
    private ImageView img_setting_bg;
    private ImageView img_head_bg;
    private ImageView img_head;
    private ImageView img_viptrue;
    private ImageView img_night;
    private ImageView img_vipicon;
    private LinearLayout ll_recharge;
    private LinearLayout ll_purchase;
    private LinearLayout ll_vipinfo;
    private LinearLayout ll_buy;

    private LinearLayout ll_bg_vip;
    private LinearLayout ll_kefu;
    private LinearLayout ll_bg_top_one;
    private LinearLayout ll_bg_top_two;
    private LinearLayout ll_interest;
    private LinearLayout ll_bg_heads;

    private TextView tv_name;
    private TextView tv_customerno;
    private TextView tv_vipName;
    private TextView tv_youxiaoqi;
    private TextView tv_shengyu;
    private int tabPostition;

    private final String[] TAB_TITLES = new String[]{"头像装饰", "主页背景", "阅读背景"};
    /**
     * 页卡适配器
     */
    private PagerAdapter adapter;

    private ConstraintLayout clHead;
    private UserProfileRequest response;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_my, container, false);
            clHead = view.findViewById(R.id.clHead);

            viewPager = view.findViewById(R.id.view_my_pager);
            tabLayout = view.findViewById(R.id.tab_my_layout);
            img_setting = view.findViewById(R.id.img_setting);
            img_setting_bg = view.findViewById(R.id.img_setting_bg);
            ll_recharge = view.findViewById(R.id.ll_recharge);
            ll_purchase = view.findViewById(R.id.ll_purchase);
            ll_vipinfo = view.findViewById(R.id.ll_vipinfo);
            ll_interest = view.findViewById(R.id.ll_interest);
            ll_bg_heads = view.findViewById(R.id.ll_bg_heads);

            ll_kefu = view.findViewById(R.id.ll_kefu);
            ll_buy = view.findViewById(R.id.ll_buy);
            img_head_bg = view.findViewById(R.id.img_head_bg);
            img_head = view.findViewById(R.id.img_head);
            tv_name = view.findViewById(R.id.tv_name);
            tv_customerno = view.findViewById(R.id.tv_customerno);
            tv_vipName = view.findViewById(R.id.tv_vipName);
            img_vipicon = view.findViewById(R.id.img_vipicon);
            tv_youxiaoqi = view.findViewById(R.id.tv_youxiaoqi);
            tv_shengyu = view.findViewById(R.id.tv_shengyu);
            ll_bg_vip = view.findViewById(R.id.ll_bg_vip);
            img_viptrue = view.findViewById(R.id.img_viptrue);
            ll_bg_top_one = view.findViewById(R.id.ll_bg_top_one);
            ll_bg_top_two = view.findViewById(R.id.ll_bg_top_two);

            img_night = view.findViewById(R.id.img_night);

            clHead.setOnClickListener(this);
            img_setting.setOnClickListener(this);
            ll_bg_top_one.setOnClickListener(this);
            img_setting_bg.setOnClickListener(this);
            ll_recharge.setOnClickListener(this);
            ll_purchase.setOnClickListener(this);
            ll_vipinfo.setOnClickListener(this);
            ll_bg_vip.setOnClickListener(this);
            ll_kefu.setOnClickListener(this);
            ll_interest.setOnClickListener(this);
            img_night.setOnClickListener(this);
            setTabs(tabLayout, getLayoutInflater(), TAB_TITLES);
            initPager();
        }
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onEvent(getContext(), UMConstant.Event_Mine_Page);

        if (PreUtils.getBoolean("tourist", false) || PreUtils.getString("RefreshToken", null) == null) {
            ll_bg_top_one.setVisibility(View.VISIBLE);
            ll_bg_top_two.setVisibility(View.GONE);
            response = null;
        } else {
            ll_bg_top_one.setVisibility(View.GONE);
            ll_bg_top_two.setVisibility(View.VISIBLE);
            //加载用户信息
            getUserInfo();
        }

//        if (isRefresh) {
//        getUserInfo();
//            isRefresh = false;
//        }
        /*if (PreUtils.getInt("app_bg", 0) == 0) {
            ll_bg_vip.setBackgroundResource(R.mipmap.my_bg_one);
            ll_bg_heads.setBackgroundResource(R.mipmap.vip_bg_one);
        } else if (PreUtils.getInt("app_bg", 0) == 2) {
            ll_bg_vip.setBackgroundResource(R.mipmap.my_bg_two);
            ll_bg_heads.setBackgroundResource(R.mipmap.vip_bg_two);
        } else if (PreUtils.getInt("app_bg", 0) == 1) {
            ll_bg_vip.setBackgroundResource(R.mipmap.my_bg_three);
            ll_bg_heads.setBackgroundResource(R.mipmap.vip_bg_three);
        } else {
            ll_bg_vip.setBackgroundResource(R.mipmap.my_bg_one);
            ll_bg_heads.setBackgroundResource(R.mipmap.vip_bg_moren);
        }
        if (PreUtils.getInt("head_bg", 0) == 0) {
            img_head.setVisibility(View.VISIBLE);
            img_head.setImageResource(R.mipmap.vip_head_quan_one);
        } else if (PreUtils.getInt("head_bg", 0) == 1) {
            img_head.setVisibility(View.VISIBLE);
            img_head.setImageResource(R.mipmap.vip_head_quan_two);
        } else if (PreUtils.getInt("head_bg", 0) == 2) {
            img_head.setVisibility(View.VISIBLE);
            img_head.setImageResource(R.mipmap.vip_head_quan_three);
        }else {
            img_head.setVisibility(View.INVISIBLE);
        }
        if (SharedPreUtils.getInstance().getBoolean(ReadSettingManager.SHARED_READ_NIGHT_MODE, false)) {
            img_night.setImageResource(R.mipmap.rijian_mode);
        } else {
            img_night.setImageResource(R.mipmap.my_heibai);
        }*/
    }

    private void getUserInfo() {
        List<UserTotalresponse.DataBean> list = new ArrayList<>();
        ((FFActivity) getActivity()).post(Url.userAccountTotalData, null, UserTotalresponse.class, new FFNetWorkCallBack<UserTotalresponse>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(UserTotalresponse response) {
                if (response.getCode() == 200) {
                    tv_shengyu.setText("累计节省" + StUtils.percentage(response.getData().getSaveAmount()) + "元");
                    UserTotalresponse.DataBean b1 = new UserTotalresponse.DataBean();
                    b1.setSaveAmount(response.getData().getSaveAmount());
                    b1.setConsumerAmount(response.getData().getConsumerAmount());
                    b1.setReadingBookCount(response.getData().getReadingBookCount());
                    b1.setReadingTime(response.getData().getReadingTime());
                    UserTotalresponse.DataBean b2 = new UserTotalresponse.DataBean();
                    b2.setSaveAmount(response.getData().getSaveAmount());
                    b2.setConsumerAmount(response.getData().getConsumerAmount());
                    b2.setReadingBookCount(response.getData().getReadingBookCount());
                    b2.setReadingTime(response.getData().getReadingTime());
                    list.add(b1);
                    list.add(b2);
                    getBalance(list);
                }

            }

            @Override
            public boolean onFail(UserTotalresponse response) {
                return false;
            }
        });

    }

    @SuppressLint("SetTextI18n")
    private void setchongzhi(LinearLayout linearLayout, LayoutInflater inflater, List<UserTotalresponse.DataBean> tabTitles) {
        linearLayout.removeAllViews();
        for (int i = 0; i < tabTitles.size(); i++) {
            View view = inflater.inflate(R.layout.my_recharge_item, linearLayout, false);
            LinearLayout.MarginLayoutParams layoutParams = (LinearLayout.MarginLayoutParams) view.getLayoutParams();
            layoutParams.setMargins(dp2px(10), 0, 0, 0);
            view.setLayoutParams(layoutParams);
            TextView tv_account = view.findViewById(R.id.tv_account);
            TextView tv_buy = view.findViewById(R.id.tv_buy);
            TextView tv_time_v = view.findViewById(R.id.tv_time_v);
            TextView tv_read_v = view.findViewById(R.id.tv_read_v);
            TextView tv_xiaofei_v = view.findViewById(R.id.tv_xiaofei_v);
            tv_buy.setText(tabTitles.get(i).getType());
            if (i == 1) {
                tv_account.setText(tabTitles.get(i).getTime());
            } else {
                tv_account.setText("我的账户：" + tabTitles.get(i).getBalance() + "完结币");
            }
            tv_time_v.setText(tabTitles.get(i).getReadingTime() / 60 + "小时" + tabTitles.get(i).getReadingTime() % 60 + "分钟");
            tv_read_v.setText(tabTitles.get(i).getReadingBookCount() + "本");
            tv_xiaofei_v.setText(StUtils.percentage(tabTitles.get(i).getConsumerAmount()) + "元");
            linearLayout.addView(view);
            int finalI = i;
            tv_buy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalI == 0) {
                        Intent intent = new Intent(getActivity(), RechargeCoinActivity.class);
                        startActivity(intent);
                        MobclickAgent.onEvent(getContext(), UMConstant.MY_SPEND_ANDROID_CLICK);
                    } else {
                        BuyMonthlyPop.bugMonthly(v, getActivity(), new BuyMonthlyPop.BuyMonthPopListener() {
                            @Override
                            public void onOkClick(String group) {
                                getUserInfo();
                            }
                        });
                        MobclickAgent.onEvent(getContext(), UMConstant.BAO_YUE_ANDROID_CLICK);
                    }
                }
            });

            // 青少年模式，屏蔽充值/购买包月按钮
            if (!PreUtils.getBoolean("QING_switch", false)) {
                tv_buy.setVisibility(View.VISIBLE);
            } else {
                tv_buy.setVisibility(View.INVISIBLE);
            }
        }
    }


    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees) {
        for (int i = 0; i < tabTitlees.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.tab_item_current, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_stack_tab);
            tvTitle.setText(tabTitlees[i]);
            tabLayout.addTab(tab);
        }
    }

    private void initPager() {
        adapter = new MyAdapter(getChildFragmentManager(), viewPager);
        viewPager.setAdapter(adapter);
        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(0).getCustomView();
        TextView textView = (TextView) linearLayout.getChildAt(0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        // 关联切换
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                viewPager.setCurrentItem(tab.getPosition(), true);
                viewPager.resetHeight(tab.getPosition());
                tabPostition = tab.getPosition();
                int s = tabLayout.getTabCount();
                for (int i = 0; i < s; i++) {
                    if (tab.getPosition() == i) {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        LinearLayout linearLayout = (LinearLayout) tabLayout.getTabAt(i).getCustomView();
                        TextView textView = (TextView) linearLayout.getChildAt(0);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
                switch (tab.getPosition()) {
                    case 0:
                        MobclickAgent.onEvent(getContext(), UMConstant.VIP_FEEL_HEADSHOT_ANDROID_CLICK);
                        break;
                    case 1:
                        MobclickAgent.onEvent(getContext(), UMConstant.VIP_FEEL_HOME_PAGE_ANDROID_CLICK);
                        break;
                    case 2:
                        MobclickAgent.onEvent(getContext(), UMConstant.VIP_FEEL_READ_BACKGROUND_ANDROID_CLICK);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        if (PreUtils.getBoolean("tourist", false)) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            return;
        }
        if (v.getId() == R.id.clHead) {
            if (response == null) return;

            Intent intent = new Intent();
            intent.setClass(getActivity(), SelectAvatarActivity.class);
            intent.putExtra(Constants.ProfileImgUrl, response.getData().getProfileImgUrl());
            intent.putExtra(Constants.backgroundBox, response.getData().getBackgroundBox());
            startActivity(intent);
        }
        else if (v.getId() == R.id.img_setting || v.getId() == R.id.img_setting_bg) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), SettingActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.ll_recharge) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), RechargeActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.ll_purchase) {
            Intent intent = new Intent();
            intent.setClass(getActivity(), MyPurchaseActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.ll_vipinfo) {
            MobclickAgent.onEvent(getContext(), UMConstant.Event_Mine_CustomBg_Click);
            /*Intent intent = new Intent();
            intent.setClass(getActivity(), VipInfoActivity.class);
            startActivity(intent);*/
            gotoVipInfo();
            MobclickAgent.onEvent(getContext(), UMConstant.PLAY_BACKGROUND_ANDROID_CLICK);
        } else if (v.getId() == R.id.ll_bg_vip) {
            MobclickAgent.onEvent(getContext(), UMConstant.Event_Mine_CustomBg_Click);
            /*Intent intent = new Intent();
            intent.setClass(getActivity(), VipInfoActivity.class);
            intent.putExtra("tab", tabPostition + "");
            startActivity(intent);*/
            gotoVipInfo();
        } else if (v.getId() == R.id.ll_kefu) {
            Intent intent = new Intent(getActivity(), WebActivity.class);
            intent.putExtra("WEB_URL", Url.help);
            intent.putExtra("WEB_NAME", "客服与帮助");
            startActivity(intent);
            MobclickAgent.onEvent(getContext(), UMConstant.SAY_HELP_ANDROID_CLICK);
        } else if (v.getId() == R.id.ll_bg_top_one) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.ll_interest) {
            new InterestPop().interestPopWindow(v, getActivity(), new InterestPop.PopInterestListener() {
                @Override
                public void onValueClick(ListUserBookRequest value) {

                }
            });
            MobclickAgent.onEvent(getContext(), UMConstant.READ_LIKE_ANDROID_CLICK);
        } else if (v.getId() == R.id.img_night) {
            if (SharedPreUtils.getInstance().getBoolean(ReadSettingManager.SHARED_READ_NIGHT_MODE, false)) {
                SharedPreUtils.getInstance().putBoolean(ReadSettingManager.SHARED_READ_NIGHT_MODE, false);
                img_night.setImageResource(R.mipmap.my_heibai);
                ((FFBaseActivity) getActivity()).setFlCoverView(false);
            } else {
                img_night.setImageResource(R.mipmap.rijian_mode);
                SharedPreUtils.getInstance().putBoolean(ReadSettingManager.SHARED_READ_NIGHT_MODE, true);
                ((FFBaseActivity) getActivity()).setFlCoverView(true);
            }
        }

    }


    public void getBalance(List<UserTotalresponse.DataBean> list) {

        ((FFActivity) getActivity()).post(Url.balance, null, UserBalanceResponse.class, new FFNetWorkCallBack<UserBalanceResponse>() {
            @Override
            public void onSuccess(UserBalanceResponse response) {
                if (response.getCode() == 200) {
                    list.get(0).setBalance(response.getData().getBalance() + "");
                    getUserProfile(list);
                }

            }

            @Override
            public boolean onFail(UserBalanceResponse response) {
                return false;
            }
        });


    }

    public void getUserProfile(List<UserTotalresponse.DataBean> list) {

        ((FFActivity) getActivity()).post(Url.getUserProfile, null, UserProfileRequest.class, new FFNetWorkCallBack<UserProfileRequest>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onSuccess(UserProfileRequest response) {
                if (response.getCode() == 200) {
                    UserProfileRequest.DataBean.MonthSubscribeInfoBean moth = response.getData().getMonthSubscribeInfo();
                    UserProfileRequest.DataBean.VipInfoBean vipInfoBean = response.getData().getVipInfo();
                    tv_name.setText(response.getData().getNickname());
                    tv_customerno.setText("客户号 " + response.getData().getUserNumber());
                    list.get(0).setType("充值");
                    list.get(1).setType("购买包月");
                    //是vip 是包月
                    if (vipInfoBean.isIsVip() && moth.isIsMonthSubscribe()) {
                        img_viptrue.setVisibility(View.VISIBLE);
                        tv_vipName.setText("VIP特权生效中");
                        tv_youxiaoqi.setText("剩余" + FFUtils.getBetweenTimeDay(vipInfoBean.getExpiredTime()) + "天  有效期到" + FFUtils.stringToDate(vipInfoBean.getExpiredTime() + ""));
                        list.get(1).setMonth(true);
                        list.get(1).setTime("我的包月：" + FFUtils.stringToDate(moth.getExpiredTime() + "") + "到期");

                    }
                    //是vip，但是没包月了
                    else if (vipInfoBean.isIsVip() && !moth.isIsMonthSubscribe()) {
                        img_viptrue.setVisibility(View.VISIBLE);
                        list.get(1).setMonth(false);
                        list.get(1).setTime("我的包月：欢迎购买");
                        tv_vipName.setText("VIP特权生效中");
                        tv_youxiaoqi.setText("有效期" + FFUtils.stringToDate(vipInfoBean.getExpiredTime() + "")
                                + "  剩余" + FFUtils.getBetweenTimeDay(vipInfoBean.getExpiredTime()) + "天");
                    }

                    //不是是vip，有包月
                    else if (!vipInfoBean.isIsVip() && moth.isIsMonthSubscribe()) {
                        img_viptrue.setVisibility(View.GONE);
                        list.get(1).setMonth(true);
                        list.get(1).setTime("我的包月：" + FFUtils.stringToDate(moth.getExpiredTime() + "") + "到期");
                        tv_vipName.setText("万本包月书免费读");
                        tv_youxiaoqi.setText("包月有效期" + FFUtils.stringToDate(moth.getExpiredTime() + "")
                                + "  剩余" + FFUtils.getBetweenTimeDay(moth.getExpiredTime()) + "天");
                    }
                    //不是vip，没有包月
                    else if (!vipInfoBean.isIsVip() && !moth.isIsMonthSubscribe()) {
                        img_viptrue.setVisibility(View.GONE);
                        list.get(1).setMonth(false);
                        list.get(1).setTime("我的包月：欢迎购买");
                        tv_vipName.setText("再消费" + StUtils.percentage(vipInfoBean.getDistanceMoney()) + "元,尊享VIP特权");
                        tv_youxiaoqi.setText("30天累计消费10元，成为VIP，购书六折，专享装扮");
                    }

                    //加载账户信息
                    setchongzhi(ll_buy, getLayoutInflater(), list);

                    MyFragment.this.response = response;

                    setAllBg(response);
                }


            }

            @Override
            public boolean onFail(UserProfileRequest response) {
                return false;
            }
        });


    }

    private void gotoVipInfo() {
        if (PreUtils.getBoolean("tourist", false) || StUtils.isEmpty(PreUtils.getString("token", ""))) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            return;
        }

        Intent intent = new Intent();
        intent.setClass(getActivity(), VipInfoActivity.class);
        intent.putExtra("tab", tabPostition + "");

        if (response != null && response.getData() != null) {
            intent.putExtra(Constants.ProfileImgUrl, response.getData().getProfileImgUrl());
            intent.putExtra(Constants.backgroundBox, response.getData().getBackgroundBox());
            intent.putExtra(Constants.Theme, response.getData().getTheme());
            intent.putExtra(Constants.Background, response.getData().getBackground());
        }
        startActivity(intent);
    }


    /**
     * 设置背景相关
     * @param response
     */
    private void setAllBg(UserProfileRequest response) {
        // 設置頭像
        if (!TextUtils.isEmpty(response.getData().getProfileImgUrl())) {
            for (int i = 0; i < UserInfoBg.INSTANCE.getUSER_ICON().length; i++) {
                if (response.getData().getProfileImgUrl().equals(UserInfoBg.INSTANCE.getUSER_ICON()[i])) {
                    img_head_bg.setImageResource(UserInfoBg.INSTANCE.getUSER_ICON_RID()[i]);
                }
            }
        }

        // 設置頭像框
        img_head.setVisibility(View.INVISIBLE);
        if (!TextUtils.isEmpty(response.getData().getBackgroundBox())) {
            for (int i = 0; i < UserInfoBg.INSTANCE.getBACKGROUND_BOX().length; i++) {
                if (response.getData().getBackgroundBox().equals(UserInfoBg.INSTANCE.getBACKGROUND_BOX()[i])) {
                    img_head.setVisibility(View.VISIBLE);
                    img_head.setImageResource(UserInfoBg.INSTANCE.getBACKGROUND_BOX_D_RID()[i]);
                }
            }
        }

        // 設置APP背景
        if (!TextUtils.isEmpty(response.getData().getBackground())) {
            for (int i = 0; i < UserInfoBg.INSTANCE.getBACKGROUND().length; i++) {
                if (response.getData().getBackground().equals(UserInfoBg.INSTANCE.getBACKGROUND()[i])) {
                    ll_bg_heads.setBackgroundResource(UserInfoBg.INSTANCE.getBACKGROUND_RID()[i]);

                    // 通知其他界面更新 App 背景
                    LiveEventBus.get(Constants.Background).post(i);
                }
            }
        } else {    // 默认背景
            ll_bg_heads.setBackgroundResource(UserInfoBg.INSTANCE.getBACKGROUND_RID()[UserInfoBg.INSTANCE.getBACKGROUND_RID().length - 1]);
            // 通知其他界面更新 App 背景
            LiveEventBus.get(Constants.Background).post(UserInfoBg.INSTANCE.getBACKGROUND_RID().length - 1);
        }

        /*for (int i = 0; i < UserInfoBg.INSTANCE.getTHEME().length; i++) {
            if (response.getData().getTheme().equals(UserInfoBg.INSTANCE.getTHEME_RID()[i])) {
                // 通知其他界面更新 閲讀 背景
                LiveEventBus.get(Constants.Theme).post(i);
            }
        }*/

        if (SharedPreUtils.getInstance().getBoolean(ReadSettingManager.SHARED_READ_NIGHT_MODE, false)) {
            img_night.setImageResource(R.mipmap.rijian_mode);
        } else {
            img_night.setImageResource(R.mipmap.my_heibai);
        }
    }

    //直接传一个float的单位为dp的值，然后方法会返回单位为px的数，直接设置即可
    public int dp2px(float dpValue) {
        final float scale = getActivity().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
